/**
 * 保存文章
 */
function subArticle(status) {
    let title = $('input[name=title]').val();
    let categorySeq = $('#categoryId').data('zui.picker').value;
    let coverImage = "/user/img/rand/3.jpg";
    let editorType = "html";
    let content = "";
    if (editorType === "html") {
        content = UE.getEditor('editor').getContent();
    }
    let params = {
        title: title,
        summary: $("#summary").val(),
        content: content,
        hasPayment: false,
        hasResource: false,
        hasLimit: false,
        status: status,
        coverImage: coverImage,
        fileSeqs: [],
        categorySeq: categorySeq,
        documentType: "md",
        labels: $("#labels").inputTags(),
        allowLikes: $("#allowLikes").prop("checked"),
        allowComment: $("#allowComment").prop("checked"),
        selfView: $("#selfView").prop("checked"),
    }
    let seqs = $('#seqs').val();
    if (seqs != null) {
        params.fileSeqs.push(seqs)
        params.hasResource = true;
    }
    beforeSave(params).then(res => {
        params.coverImage = res;
        console.log(params)
        request({
            url: baseUrl + "/api/article",
            method: 'post',
            data: params,
        }).then((response) => {
            console.log(response)
            new $.zui.Messager('文章创建成功', {
                icon: 'heart',
                placement: 'center'
            }).show();
        }).catch(function (error) {
            console.log(error);
        });
    }).catch(res => {

    })
}

function beforeSave(params) {
    return new Promise((resolve, reject) => {
        if (window.FsImgUploader) {
            let formData = new FormData();
            formData.append("file", FsImgUploader.resources[0].imgFile);
            request({
                url: baseUrl + "/api/article/image",
                headers: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"},
                method: 'post',
                data: formData,
            }).then((response) => {
                let imageUrl = response.data;
                resolve(imageUrl);
            }).catch(function (error) {
                console.log(error);
            });
        }

    });
}

let textarea = $('#text');
let toolbar = $('<div class="markdown-editor" id="md-button-bar" />').insertBefore(textarea.parent());
let preview = $('<div id="md-preview" class="md-hidetab" />').insertAfter('.markdown-editor');
markdown(textarea, toolbar, preview);